﻿using System.Collections.Generic;
using System.IO;
using JetBlack.IO;
using JetBlack.Text;

namespace JetBlack.TopicBus.Messages
{
    internal class SubscriberMessage : Message
    {
        public readonly string Topic;
        public readonly bool IsImage;
        public readonly IDictionary<string, object> Data;

        public SubscriberMessage(string topic, bool isImage, IDictionary<string, object> data)
            : base(MessageType.SubscriberMessage)
        {
            Topic = topic;
            IsImage = isImage;
            Data = data;
        }

        new static public SubscriberMessage Read(Stream reader)
        {
            string topic = reader.ReadString();
            bool isImage = reader.ReadBoolean();
            IDictionary<string, object> data = reader.ReadDictionary();
            return new SubscriberMessage(topic, isImage, data);
        }

        public override Stream Write(Stream writer)
        {
            base.Write(writer);
            writer.Write(Topic);
            writer.Write(IsImage);
            writer.Write(Data);
            return writer;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} [{3}]", MessageType, Topic, IsImage, Data.ToDelimitedString());
        }
    }
}
